﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Web.UserControls;
using Telerik.Web.UI;
using BSCMFF.Core.BusinessLayer;
using BSCMFF.Core.Exceptions.Database;
using BSCMFF.Core;
using BSCMFF.Web.UserControls.Evaluation;
using BSCMFF.Web.UserControls.Details;
using BSCMFF.Core.EmailProvider;

namespace BSCMFF.Web.Pages.Evaluation_Setup
{
    public partial class EvaluationFillingMode : MultiLanguagePage
    {
        private static int eeeID;
        private static Evaluation_Evaluator_Employee _eee;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            CheckSession();   
            //EmployeeSelection1.EventEmployeeChanged += new EventHandler(EmployeeSelection1_EventEmployeeChanged);
            BSCEntities context = new BSCEntities();

            if (Request.QueryString["EvaluationEvaluatorEmployee"] == null) //preview evaluation
            {
                if (Request.QueryString["EvaluationID"] == null)
                {    //Response.Redirect("EvaluationFillingMode.aspx?EvaluationID=19");
                    RadButtonBack.Visible = true;
                    RadButtonCancel.Visible = false;
                    RadButtonConfirmInterview.Visible = false;
                    RadButtonDelete.Visible = false;
                    RadButtonEvaluate.Visible = false;
                    RadButtonSave.Visible = false;
                    return;
                    
                }
                else
                {
                    int evalID = Int32.Parse(Request.QueryString["EvaluationID"]);
                    Evaluation_Period eps = (from ep in context.Evaluation_Period
                                             where ep.ID == evalID
                                             select ep).SingleOrDefault();
                    ed.Evalut = eps;
                    ed.DataBind();

                    PlaceHolderInterviewPart.Visible = eps.Interview == null ? false : (bool)eps.Interview;
                    PlaceHolderPKPIsPart.Visible = eps.PKPIs == null ? false : (bool)eps.PKPIs;
               //     PanelRewardPKPISettings.Visible = eps.PKPIs == null ? false : (bool)eps.PKPIs;
                    PlaceHolderCompetenciesPart.Visible = eps.Competencies == null ? false : (bool)eps.Competencies;
               //     PanelRewardCompetenciesSetting.Visible = eps.Competencies == null ? false : (bool)eps.Competencies;
                    PlaceHolderRewardPart.Visible = eps.Reward == null ? false : (bool)eps.Reward;

                    

                    if (eps.Competencies == true)
                    {
                        LoadCategories(eps.ID);
                    }

                    RadTextBoxEmployeeReaction.Enabled = false;
                    RadTextBoxOveralEvaluatorEval.Enabled = false;

                    DisableRadioButtons();
                    //CheckBoxTestOnly.Enabled = false;
                    //PanelRewardCompetenciesSetting.Enabled = false;
                    //PanelRewardPKPISettings.Enabled = false;
                    PanelPKPIs.Enabled = false;
                    //Cascade.en

                    RadButtonEvaluate.Visible = false;
                    RadButtonDelete.Visible = false;
                    RadButtonSave.Visible = false;
                    RadButtonConfirmInterview.Visible = false;
                    RadButtonBack.Visible = true;
                    RadButtonCancel.Visible = false;
                    //Cascade.PublicButtonCascadeEnabled = false;
                }

            }
            eeeID = Convert.ToInt32(Request.QueryString["EvaluationEvaluatorEmployee"]);


            
            _eee = (from eee in context.Evaluation_Evaluator_Employee.Include("Evaluation_Period").Include("User").Include("User1")
                    where eee.ID == eeeID
                    select eee).SingleOrDefault();
            if (_eee == null)
            {
                //Response.Redirect("EvaluationFillingMode.aspx");
                return;
            }

            
            ed.Evalut = _eee.Evaluation_Period;
            ed.DataBind();
            //ed.Evalut = selectedEval;
            //this.Evalut = selectedEval;
            //this.DataBind();
            HiddenFiledEmployeeID.Value = _eee.User.ID.ToString();
            SetEmployee(_eee.User);
            SetEvaluator(_eee.User1);

            LabelCurrentDateOfInterview.Text = DateTime.Today.ToShortDateString();

            PlaceHolderInterviewPart.Visible = _eee.Evaluation_Period.Interview == null ? false : (bool)_eee.Evaluation_Period.Interview;
            PlaceHolderPKPIsPart.Visible = _eee.Evaluation_Period.PKPIs == null ? false : (bool)_eee.Evaluation_Period.PKPIs;
            PlaceHolderCompetenciesPart.Visible = _eee.Evaluation_Period.Competencies == null ? false : (bool)_eee.Evaluation_Period.Competencies;

            if (Request.QueryString["Step"] == "Interview")
            {
                
                SetInterviewStep();
            }
            else
            {

                SetEvalutationStep();
            }
        }

        private void SetEvalutationStep()
        {
            if (_eee.Evaluation_Period.Competencies == true)
            {
                LoadCategories(_eee.Evaluation_Period.ID);
            }

            if (_eee.Evaluation_Period.PKPIs == true)
            {
                LoadPKPIs(_eee.ID, _eee.User.ID);
            }

            if (Request.QueryString["Mode"] != null)
            {
                SetMode(Request.QueryString["Mode"]);
            }
            else
            {
                RadButtonSave.Visible = false;
                RadButtonDelete.Visible = false;
                //Cascade.Visible = false;
                //RadTextBoxEmployeeReaction.Enabled = false;
                //RadTextBoxOveralEvaluatorEval.Enabled = false;
                
                LabelCurrentDateOfInterview.Text = ((DateTime)_eee.DateFilled).ToShortDateString();
                
            }
            RadButtonConfirmInterview.Visible = false;

            PanelScale.Enabled = false;

            RadGridPersonalKPIs.Visible = false;
            RadButtonAssignEvenWeights.Visible = false;
            RadTextBoxEmployeeReaction.Enabled = false;

            //RadTextBoxOveralEvaluatorEval.Text = _eee.OveralEvaluatorEval;
            //RadTextBoxEmployeeReaction.Text = _eee.EmployeeReaction;

            if (!IsPostBack)
            {
                RadTextBoxOveralEvaluatorEval.Text = _eee.OveralEvaluatorEval;
            }
        }

        private void LoadPKPIs(int eeeID, int userID)
        {
            BSCEntities context = new BSCEntities();

            //List<Personal_KPI> pkpis = (from pkpi in context.Personal_KPI.Include("Evaluation_PKPIs")
            //                            where pkpi.User_FK == userID && pkpi.Evaluation_PKPIs.FirstOrDefault().Evaluation_FK == evalID
            //                            select pkpi).ToList();

            List<Evaluation_PKPIs> epkpis = (from epkpi in context.Evaluation_PKPIs.Include("Personal_KPI")
                                             where epkpi.Personal_KPI.User_FK == userID && epkpi.EEE_FK == eeeID
                                             select epkpi).ToList();

            foreach (Evaluation_PKPIs p in epkpis)
            {

                EvaluatePKPIDetail ep = LoadControl("~/UserControls/Evaluation/EvaluatePKPIDetail.ascx") as EvaluatePKPIDetail;
                ep.EPKPI = p;
                ep.DataBind();

                PlaceHolderEvaluatePKPIs.Controls.Add(ep);
            }
        }

        private void SetInterviewStep()
        {
            if (_eee.Evaluation_Period.Competencies == true)
            {
                LoadCategories(_eee.Evaluation_Period.ID);
            }
            DisableRadioButtons();
            RadTextBoxEmployeeReaction.Enabled = false;
            RadTextBoxOveralEvaluatorEval.Enabled = false;

            //PanelRewardPKPISettings.Visible = _eee.Evaluation_Period.PKPIs == null ? false : (bool)_eee.Evaluation_Period.PKPIs;            
            //PanelRewardCompetenciesSetting.Visible = _eee.Evaluation_Period.Competencies == null ? false : (bool)_eee.Evaluation_Period.Competencies;
            PlaceHolderRewardPart.Visible = _eee.Evaluation_Period.Reward == null ? false : (bool)_eee.Evaluation_Period.Reward;


            //CheckBoxTestOnly.Enabled = false;
            //PanelRewardCompetenciesSetting.Enabled = false;
            //PanelRewardPKPISettings.Enabled = false;
            //PanelPKPIs.Enabled = false;
            PanelScale.Visible = false;

            //Cascade.PublicButtonCascadeEnabled = false

            RadButtonEvaluate.Visible = false;
            RadButtonDelete.Visible = false;
            RadButtonSave.Visible = false;
            RadButtonConfirmInterview.Visible = true;
            PlaceHolderEvaluatePKPIs.Visible = false;
        }

        private void SetMode(string mode)
        {
            //Cascade.Visible = false;
            switch (mode)
            {
                case "Select": DisableRadioButtons(); DisableTextBoxes(); ShowSelectButtons(); RadTextBoxOveralEvaluatorEval.Enabled = false; break;
                case "Edit":  ShowEditButtons(); break;
                default: break;
            }
        }

        private void DisableTextBoxes()
        {
            foreach (EvaluatePKPIDetail epd in PlaceHolderEvaluatePKPIs.Controls)
            {
                epd.DisableTextBoxes();
            }

            
            //foreach (Control c in PlaceHolderRewardPart.Controls)
            //{
            //    if (c.GetType().Name == "RadTextBox")
            //    {
            //        ((RadTextBox)c).Enabled = false;
            //    }
            //    if (c.GetType().Name == "RadNumericTextBox")
            //    {
            //        ((RadNumericTextBox)c).Enabled = false;
            //    }
            //}
        }

        private void ShowEditButtons()
        {
            RadButtonEvaluate.Visible = false;

        }

        

        private void ShowSelectButtons()
        {
            RadButtonEvaluate.Visible = false;
            RadButtonSave.Visible = false;
            RadButtonDelete.Visible = false;
            RadButtonCancel.Text = "Back";
        }

        private void DisableRadioButtons()
        {
            foreach (EvaluationQuestionCategoryEvalMode eqcem in PlaceHolderCategories.Controls)
            {
                eqcem.DisabledValues = true;
            }


        }

        private void SetEvaluator(User evaluator)
        {
            DepartmentManager dm = new DepartmentManager();

            LabelCurrentEvaluatorFullname.Text = evaluator.Fullname;
            if (evaluator.SBU_FK != null)
            {
                LabelCurrentEvaluatorDepartment.Text = dm.GetDepartmentByID((int)evaluator.SBU_FK).Name;
            }
            else
            {
                LabelCurrentEvaluatorDepartment.Text = "";
            }
            LabelCurrentEvaluatorJobPosition.Text = evaluator.Job_Position;
        }

        private void SetEmployee(User u)
        {
            DepartmentManager dm = new DepartmentManager();

            LabelCurrentFullname.Text = u.Fullname;
            if (u.SBU_FK != null)
            {
                LabelCurrentDepartment.Text = dm.GetDepartmentByID((int)u.SBU_FK).Name;
            }
            else
            {
                LabelCurrentDepartment.Text = "";
            }
            LabelCurrentJobPosition.Text = u.Job_Position;
            
        }

        private void LoadCategories(int evalID)
        {
            BSCEntities context = new BSCEntities();

            List<Evaluation_Category> cats = (from c in context.Evaluation_Category
                                              where c.Evaluation_FK == evalID
                                              // orderby c.ID descending
                                              select c).ToList();
            int categories = 0;
            foreach (Evaluation_Category cat in cats)
            {
                EvaluationQuestionCategoryEvalMode eqc = LoadControl("~/UserControls/EvaluationQuestionCategoryEvalMode.ascx") as EvaluationQuestionCategoryEvalMode;
                eqc.ID = "EQCEM" + categories; //cat.ID.ToString();
                eqc.CategoryName = cat.Name;

                List<Evaluation_Question> equestions = (from eq in context.Evaluation_Question//.Include("Evaluation_User_Question_Value")
                                                        where eq.Evaluation_Category_FK == cat.ID // && (eq.Evaluation_User_Question_Value.Count == 0 || eq.Evaluation_User_Question_Value.FirstOrDefault().EEE_FK == eeeID)

                                                        select eq).ToList();
                foreach (Evaluation_Question equestion in equestions)
                {
                    equestion.Evaluation_User_Question_Value.Clear();
                    int qID = equestion.ID;
                    Evaluation_User_Question_Value value = (from v in context.Evaluation_User_Question_Value
                                                            where v.Evaluation_Question_FK == qID && v.EEE_FK == eeeID
                                                            select v).FirstOrDefault();

                    if (value != null)
                    {
                        equestion.Evaluation_User_Question_Value.Add(value);
                    }
                    //context.LoadProperty(equestion, );
                }

                if (equestions != null)
                {

                    eqc.SavedQuestions = equestions;
                    eqc.Min = cat.Min;
                    eqc.Max = cat.Max;

                    PlaceHolderCategories.Controls.AddAt(categories++, eqc);
                }
            }
        }

        protected void RadButtonEvaluate_Click(object sender, EventArgs e)
        {
            BSCEntities context = new BSCEntities();
            //List<Evaluation_User_Question_Value> toAdd = new List<Evaluation_User_Question_Value>();

            Evaluation_Evaluator_Employee fillEEE = (from fe in context.Evaluation_Evaluator_Employee.Include("Evaluation_Period").Include("User")
                                                     where fe.ID == eeeID
                                                     select fe).SingleOrDefault();

            fillEEE.Evaluated = true;
            fillEEE.DateEvaluated = DateTime.Today;
            fillEEE.OveralEvaluatorEval = RadTextBoxOveralEvaluatorEval.Text;

            //save PKPIs score
            foreach (EvaluatePKPIDetail epd in PlaceHolderEvaluatePKPIs.Controls)
            {
                Evaluation_PKPIs epk = (from ep in context.Evaluation_PKPIs
                                       where ep.ID == epd.EPKPI_ID
                                       select ep).SingleOrDefault();

                if (epk != null)
                {
                    epk.Score = epd.Score;
                    epk.VerbalEvaluation = epd.VerbalEvaluation;
                    epk.Note = epd.Note;

                }
            }
            //save competencies
            foreach (EvaluationQuestionCategoryEvalMode eqcem in PlaceHolderCategories.Controls)
            {
                if (eqcem.Visible == false)
                    break;

                List<Evaluation_User_Question_Value> returned = eqcem.GetEvaluationValues();
                foreach (Evaluation_User_Question_Value euqv in returned)
                {
                    euqv.EEE_FK = eeeID;

                    context.AddToEvaluation_User_Question_Value(euqv);
                }
            }
            try
            {
                context.SaveChanges();
               
            }
            catch
            {
                throw new InconsistentDatabaseException("Couldn't update evaluation");
            }

            try
            {
                EmailProvider.SendEmail(fillEEE.User.Email, "BSCMFF Evaluation", "You have been evaluated<br />Evaluation - " + fillEEE.Evaluation_Period.Name + "<br />Date: " + DateTime.Today.ToShortDateString());

            }
            catch
            {

            }

             Response.Redirect("EvaluateEmployees.aspx?EvaluationEvaluatorEmployee=" + eeeID);


        }


        protected void RadButtonConfirmInterview_Click(object sender, EventArgs e)
        {
            int sumOfWeights = Int32.Parse(HiddenFieldTotalWeights.Value);

            if (sumOfWeights.ToString() != "100")
            {
                LabelValidatorTotalWeights.Visible = true;
                LabelValidatorTotalWeights.Focus();
                return;
            }
            else
            {
                LabelValidatorTotalWeights.Visible = true;

                BSCEntities context = new BSCEntities();
                //List<Evaluation_User_Question_Value> toAdd = new List<Evaluation_User_Question_Value>();

                Evaluation_Evaluator_Employee fillEEE = (from fe in context.Evaluation_Evaluator_Employee
                                                         where fe.ID == eeeID
                                                         select fe).SingleOrDefault();

                fillEEE.Filled = true;
                fillEEE.DateFilled = DateTime.Today;

                int userID = Int32.Parse(HiddenFiledEmployeeID.Value);
                var pkpis = (from pkpi in context.Personal_KPI
                             where pkpi.User_FK == userID
                             select pkpi).ToList();


                foreach (var p in pkpis)
                {
                    Evaluation_PKPIs eval_pkpi = new Evaluation_PKPIs();
                    eval_pkpi.EEE_FK = _eee.ID;// Evaluation_FK;
                    eval_pkpi.PKPI_FK = p.Personal_KPI_ID;
                    eval_pkpi.Weight = p.Weight;
                    context.AddToEvaluation_PKPIs(eval_pkpi);
                }

                //context.SaveChanges();

                //eval_pkpi.Weight = newPKPI.Weight;

                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    throw new InconsistentDatabaseException("Couldn't bind Personal KPI with Evaluation period");
                }



                Response.Redirect("EvaluateEmployees.aspx?EvaluationEvaluatorEmployee=" + eeeID);
            }
        }

        protected void RadButtonChange_Click(object sender, EventArgs e)
        {
            BSCEntities context = new BSCEntities();

            List<Evaluation_User_Question_Value> valuesToDelete = (from vtd in context.Evaluation_User_Question_Value
                                                                   where vtd.EEE_FK == eeeID
                                                                   select vtd).ToList();

            foreach (Evaluation_User_Question_Value ev in valuesToDelete)
            {
                context.DeleteObject(ev);
            }

            try
            {
                context.SaveChanges();
                RadButtonEvaluate_Click(sender, new EventArgs());
            }
            catch
            {
                throw new InconsistentDatabaseException("Couldn't update evaluation");
            }
            Response.Redirect("EvaluateEmployees.aspx?EvaluationEvaluatorEmployee=" + eeeID);
        }

        protected void RadButtonCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("EvaluateEmployees.aspx?EvaluationEvaluatorEmployee=" + eeeID);
        }

        protected void RadButtonDelete_Click(object sender, EventArgs e)
        {
            EvaluationManager em = new EvaluationManager();
            em.DeleteEvaluationEvaluatorEmployee(eeeID);
            RadButtonCancel_Click(sender, e);


        }
        protected void RadButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Personalist/ManageEvaluations.aspx");
        }

        #region
        


        protected void RadGridPersonalKPIs_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            PKPIDetails userControl = (PKPIDetails)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

            //int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Personal_KPI_ID"].ToString());
            BSCEntities context = new BSCEntities();

            //Personal_KPI pkpi = (from p in context.Personal_KPI
            //                     where p.Personal_KPI_ID == id
            //                     select p).SingleOrDefault();
            Personal_KPI pkpi = new Personal_KPI();

            pkpi.Name = userControl.Name;
            pkpi.Description = userControl.Description;
            pkpi.Weight = userControl.Weight;
            pkpi.Measure = userControl.Measure;
            pkpi.StartValue = (int)userControl.StartValue;
            pkpi.TargetValue = (int)userControl.TargetValue;
            pkpi.CurrentValue = (int)userControl.CurrentValue;
            pkpi.User_FK = Int32.Parse(HiddenFiledEmployeeID.Value);
            pkpi.KPI_FK = userControl.KPI_FK;
            context.AddToPersonal_KPI(pkpi);
            try
            {
                context.SaveChanges();
            }
            catch
            {
                throw new InconsistentDatabaseException("Couldn't create personal KPI");
            }


        }
        protected void RadGridPersonalKPIs_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            PKPIDetails userControl = (PKPIDetails)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);



            int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Personal_KPI_ID"].ToString());
            BSCEntities context = new BSCEntities();

            Personal_KPI pkpi = (from p in context.Personal_KPI
                                 where p.Personal_KPI_ID == id
                                 select p).SingleOrDefault();

            if (pkpi != null)
            {
                pkpi.Name = userControl.Name;
                pkpi.Description = userControl.Description;
                pkpi.Weight = userControl.Weight;
                pkpi.Measure = userControl.Measure;
                pkpi.StartValue = (int)userControl.StartValue;
                pkpi.TargetValue = (int)userControl.TargetValue;
                pkpi.CurrentValue = (int)userControl.CurrentValue;
                pkpi.KPI_FK = userControl.KPI_FK;
                //pkpi.User_FK = Convert.ToInt32((userControl.FindControl("ComboBoxUsers") as RadComboBox).SelectedValue);

                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    throw new InconsistentDatabaseException("Couldn't save personal KPI");
                }

            }

        }

        protected void RadGridPersonalKPIs_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            PKPIDetails userControl = (PKPIDetails)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);



            int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Personal_KPI_ID"].ToString());
            BSCEntities context = new BSCEntities();

            Personal_KPI pkpi = (from p in context.Personal_KPI
                                 where p.Personal_KPI_ID == id
                                 select p).SingleOrDefault();

            if (pkpi != null)
            {

                context.DeleteObject(pkpi);

                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    throw new InconsistentDatabaseException("Couldn't delete personal KPI");
                }
            }
        }

        int totalWeight = 0;
        protected void RadGridPersonalKPIs_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = e.Item as GridDataItem;
                int fieldValue = int.Parse(dataItem["Weight"].Text);
                totalWeight += fieldValue;
            }
            if (e.Item is GridFooterItem)
            {
                GridFooterItem footerItem = e.Item as GridFooterItem;
                footerItem["Weight"].Text = "Total " + totalWeight.ToString();
                HiddenFieldTotalWeights.Value = totalWeight.ToString();
                if (totalWeight == 100)
                {
                    footerItem["Weight"].ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    footerItem["Weight"].ForeColor = System.Drawing.Color.Red;
                }
            }
        }

        protected void RadGridPersonalKPIs_DataBound(object sender, EventArgs e)
        {
            if (HiddenFieldTotalWeights.Value != "0")
            {
                RadButtonAssignEvenWeights.Visible = true;
            }
            else
            {
                RadButtonAssignEvenWeights.Visible = false;
            }

            if (HiddenFieldTotalWeights.Value == "100")
            {
                LabelValidatorTotalWeights.Visible = false;
            }
        }

        protected void RadButtonAssignEvenWeights_Click(object sender, EventArgs e)
        {
            BSCEntities context = new BSCEntities();

            int userID = Int32.Parse(HiddenFiledEmployeeID.Value);

            List<Personal_KPI> pkpis = (from pkpi in context.Personal_KPI
                                        where pkpi.User_FK == userID
                                        select pkpi).ToList();
            if (pkpis.Count == 1)
            {
                pkpis[0].Weight = 100;
            }
            if (pkpis.Count == 0)
            {
                return;
            }
            int averageUp = (int)Math.Ceiling((decimal)(100 / (decimal)pkpis.Count));
            int averageDown = (int)Math.Floor((decimal)(100 / (decimal)pkpis.Count));

            int totalSoFar = 0;
            for (int i = 0; i < pkpis.Count - 1; ++i)
            {
                int average = i % 2 == 0 ? averageUp : averageDown;
                pkpis[i].Weight = average;
                totalSoFar += average;
            }
            if (pkpis.Count >= 2)
            {
                pkpis[pkpis.Count - 1].Weight = 100 - totalSoFar;
            }

            try
            {
                context.SaveChanges();
                RadGridPersonalKPIs.Rebind();
            }
            catch (Exception ex)
            {
                throw new InconsistentDatabaseException("Couldn't even weights: " + ex.Message);
            }
        }

        //protected void CheckBoxNotOkOnly_Changed(object sender, EventArgs e)
        //{
        //    LoadInferiorEmployees((int)Session["loggedUserID"]);
        //}

        //protected void RadGridUserPKPIsWeight_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        //{
        //    BSCEntities context = new BSCEntities();
        //    int loggedUserID = (int)Session["loggedUserID"];

        //    var users = (from user in context.Users
        //                 where user.Manager_FK == loggedUserID
        //                 select user).ToList();

        //    RadGridUserPKPIsWeight.DataSource = users;
        //}
        #endregion

        private void CheckSession()
        {
            if (Session["loggedUserID"] == null)
            {
                string login = HttpContext.Current.User.Identity.Name;

                BSCEntities context = new BSCEntities();

                User user = (from u in context.Users
                             where u.Login == login
                             select u).SingleOrDefault();

                if (user != null)
                {
                    Session.Add("loggedUserID", user.ID);
                }
            }
        }
    }
}